package cn.edu.zjut.action;

import cn.edu.zjut.Bean.UserBean;
import cn.edu.zjut.Service.IUserService;
import cn.edu.zjut.Service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
    private UserBean loginUser;
    private IUserService userService = null;
    public UserBean getLoginUser() {
        return loginUser;
    }
    public void setLoginUser(UserBean loginUser) {
        this.loginUser = loginUser;
    }
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    public void validateLogin() {
        String username = loginUser.getUsername();
        String pwd = loginUser.getPassword();
        if (username == null || username .equals("")) {
            this.addFieldError("loginUser.username", "请输入您的用户名！");
        }
        if (pwd == null || pwd.equals("")) {
            this.addFieldError("loginUser.password", "请输入您的密码！");
        }
    }
    public String login(){
        if (userService.login(loginUser)) {
            this.addActionMessage("登录成功！");
            return "success";
        }
        else{
            this.addActionMessage("登录失败，用户名或密码错误！");
            return "fail";
        }
    }
    public String register(){
        if (userService.register(loginUser)) {
            return "success";
        }
        else if(loginUser.getBirthday()==null){
            return "input";
        }
       else {
           return "fail";
        }
    }
}
